<?php
include("db.php");    	
require_once '../Classes/PHPExcel.php';       
require_once '../Classes/PHPExcel/Writer/Excel5.php';

define("DB_IP","127.0.0.1");//192.168.99.150
define("DB_DATABASE", "chutest");
define("DB_USERNAME", "root");
define("DB_PASSWORD","");//密码为空
function gettoken()
{
	if(!empty($_POST['token'])||!empty($_GET['token']))
	{
	  $token=empty($_POST['token'])?$_GET["token"]:$_POST["token"];		
	}
	else
	{
		$token=$_COOKIE["CCUSER"];
	}
	return $token;
}
function getdata($key){
	
	if(isset($_POST[$key]))
	{
		return $_POST[$key];
	}
	else
	{
		if(isset($_GET[$key]))
		{
			return $_GET[$key];
		}
		else
		{
			return "";
		}
	}
}

$db=new DB();

$searchKey=getdata('searchKey');
$searchDateStart=getdata('searchDateStart');
$searchDateEnd=getdata('searchDateEnd');
$nodispatch=getdata('nodispatch')==""?1:getdata('nodispatch');
$nohandle=getdata('nohandle')==""?1:getdata('nohandle');
$handled=getdata('handled')==""?1:getdata('handled');


$token=gettoken();
$uid=$db->getOne("SELECT * from chu_user where token='".$token."'");
$userType=$uid['Type'];
$uid=$uid['uid'];

$serachKey=$searchKey;
$sql_statusfilter="";
			$incontent="'',";
			if($nodispatch==1)
			{
				$incontent.="'未调度',";
			}
			if($nohandle==1)
			{
				$incontent.="'未处理',";
			}
			if($handled==1)
			{
				$incontent.="'已处理',";
			}
			
			if($incontent!="")
			{

				$incontent=substr($incontent,0,strlen($incontent)-1);
				
			}
			//$incontent="'已处理','未处理','未调度'";///test
			$sql_statusfilter.=" and Status in(".$incontent.")";
$sql_end=" order by CreateTime desc";
			//p($serachKey);
			if(trim($searchKey)=="")
			{
			if($userType=="客户")
				$sql ="SELECT * from chu_workorder where ClientId= '".$uid."' ";
			else if($userType=="调度员"){
				$sql ="SELECT * from chu_workorder where Id in (SELECT WorkOrderId from chu_routelog where HandlerId=".$uid." and Result='待调度') ";
				

			}else if($userType=="处理员"){
				$sql ="SELECT * from chu_workorder where  Id in (SELECT WorkOrderId from chu_routelog where HandlerId=".$uid." and Result='处理'  ) ";

				//p($sql."    ".$sql2);die;
			}else{
				//员工
				$sql ="SELECT * from chu_workorder where CreatePersonId= '".$uid."' ";
			}
			}
			else
			{
			if($userType=="客户")
				$sql ="SELECT * from chu_workorder where ClientId= '".$uid."' and Id like '%".$serachKey."%' ";
			else if($userType=="调度员"){
				$sql ="SELECT * from chu_workorder Id in (SELECT WorkOrderId from chu_routelog where HandlerId=".$uid." and Result='待调度' ) and Id like '%".$serachKey."%' ";


			}else if($userType=="处理员"){
				$sql ="SELECT * from chu_workorder where  Id in (SELECT WorkOrderId from chu_routelog where HandlerId=".$uid." and Result='处理'   ) and Id like '%".$serachKey."%' ";

				//p($sql."    ".$sql2);die;
			}else{
				//员工
				$sql ="SELECT * from chu_workorder where CreatePersonId= '".$uid."' and Id like '%".$serachKey."%' ";
			}
			}
			$datefilter="";
			if(trim($searchDateStart)!="")
			{
				$dateStart=strtotime($searchDateStart);
				if($searchDateEnd!="")
				$dateEnd=strtotime($searchDateEnd." 23:59:59");
				else
				$dateEnd=time();
				$datefilter=" and CreateTime>=".$dateStart."&&CreateTime<".$dateEnd;
				$sql=$sql.$datefilter;
			}
			
			$sql=$sql.$sql_statusfilter;
			$sql=$sql.$sql_end;
			$array=$db->query($sql);



$objExcel=new PHPExcel();


// 创建文件格式写入对象实例, uncomment       
$objWriter = new PHPExcel_Writer_Excel5($objExcel);      
    
//设置文档基本属性       
$objProps = $objExcel->getProperties();       
$objProps->setCreator("WK");       
$objProps->setLastModifiedBy("2015-07-16");       
$objProps->setTitle("测试");       
$objProps->setSubject("主题");       
$objProps->setDescription("这是描述");       
$objProps->setKeywords("关键字");       
$objProps->setCategory("目录"); 

$objExcel->setActiveSheetIndex(0);       
$objActSheet = $objExcel->getActiveSheet();    
$objActSheet->setTitle('工单列表');    


$character=array('','A','B','C','D','E','F','G','H','I','J','K','L','M');
$objActSheet->setCellValue("A1","工单编号");
$objActSheet->setCellValue("B1","创建人ID");
$objActSheet->setCellValue("C1","客户ID");
$objActSheet->setCellValue("D1","工单类型");
$objActSheet->setCellValue("E1","网点ID");
$objActSheet->setCellValue("F1","工单内容");
$objActSheet->setCellValue("G1","处理状态");
$objActSheet->setCellValue("H1","创建时间");
$objActSheet->setCellValue("I1","截止时间");
$objActSheet->setCellValue("J1","线路ID");
$objActSheet->setCellValue("K1","描述");

$i=2;
$j=1;
foreach($array as $list)
{
	$j=1;
	foreach($list as $k=>$v)
	{
		if($k=="CreateTime"||$k=="DateTime")
		{
			$date=date('Y-m-d H:i:s',$list[$k]);
			$objActSheet->setCellValue($character[$j].$i,$date);
		}
		else
		{
			$objActSheet->setCellValue($character[$j].$i,$list[$k]);
		}
		
		$j++;
	}
	$i++;
}


$outputFileName = "tables/"."addminus.xls";  

header("Content-Type: application/force-download"); 
   header("Content-Type: application/octet-stream"); 
   header("Content-Type: application/download"); 
   header('Content-Disposition:inline;filename="'.$outputFileName.'"'); 
   header("Content-Transfer-Encoding: binary"); 
   header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
   header("Pragma: no-cache"); 
   $objWriter->save('php://output'); 
// $objWriter->save($outputFileName); 
// echo("<a href='tables/addminus.xls' mce_href='tables/addminus.xls' target='_blank'>点击下载电子表</a>");  

